package com.dhcy.mqtt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.dhcy.mqtt.bean.TreeResource;
import com.dhcy.mqtt.config.JWTToken;
import com.dhcy.mqtt.dao.OperationLogMapper;
import com.dhcy.mqtt.dao.RedisDao;
import com.dhcy.mqtt.model.OperationLog;
import com.dhcy.mqtt.model.SysUser;
import com.dhcy.mqtt.par.ht.*;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.service.SysUserService;
import com.dhcy.mqtt.util.AppResultData;
import com.dhcy.mqtt.util.JWTUtil;
import com.dhcy.mqtt.util.Md5Util;
import com.dhcy.mqtt.util.RedisUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("sysUser")
public class SysuserController {


    public static final String salt = "afasdfgbf2132sawe";
    @Autowired
    SysUserService sysUserService;

    @Autowired
    OperationLogMapper operationLogMapper;
    @Autowired
    RedisDao redisDao;

    @PostMapping("login.json")
    @ResponseBody
    public AppResultData loginJson(@Valid HtLoginPar htLoginPar) {
        Subject subject = SecurityUtils.getSubject();
//        String pwd = Md5Util.getHtMd5(htLoginPar.getPassword());
//        String token= JWTUtil.createToken(htLoginPar.getUsername());
//        JWTToken jwtToken=new JWTToken(token);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(htLoginPar.getUsername(), htLoginPar.getPassword());
//        usernamePasswordToken.setRememberMe(true);
        subject.login(usernamePasswordToken);
        OperationLog operationLog = new OperationLog();
        operationLog.setCreateUser(htLoginPar.getUsername());
        operationLog.setCreateTime(new Date());
        operationLog.setMessage(htLoginPar.getUsername()+"登录");
        operationLogMapper.insert(operationLog);
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",htLoginPar.getUsername());
        return AppResultData.successData(map);
//        return AppResultData.successMessage("登陆成功");
    }

    @PostMapping("/user/login.json")
    @ResponseBody
    public AppResultData userLoginJson(@Valid HtLoginPar htLoginPar) {
        String pwd = Md5Util.getHtMd5(htLoginPar.getPassword());
        SysUser sysUser = sysUserService.getBaseMapper().selectOne(new QueryWrapper<SysUser>().eq("username", htLoginPar.getUsername()));
        if (sysUser == null){
            return AppResultData.errorMessage("用户不存在");
        }
        if (!pwd.equals(sysUser.getPassword())){
            return AppResultData.errorMessage("账号与密码不匹配");
        }
        String token = JWTUtil.sign(htLoginPar.getUsername(), sysUser.getPassword(), sysUser.getId().longValue());
        redisDao.putSecondValue("Constant.RM_TOKEN_CACHE" + token + StringPool.UNDERSCORE, token, 24*60*60);
        OperationLog operationLog = new OperationLog();
        operationLog.setCreateUser(htLoginPar.getUsername());
        operationLog.setCreateTime(new Date());
        operationLog.setMessage(htLoginPar.getUsername()+"登录");
        operationLogMapper.insert(operationLog);
        HashMap<String, Object> map = new HashMap<>();
        sysUser.setPassword(null);
        map.put("token",token);
        map.put("user",sysUser);
        return AppResultData.successData(map);
    }

    @GetMapping("authList.json")
    @ResponseBody
    TableResultData authList(AuthListPar authListPar) {
        return sysUserService.authList(authListPar);
    }

    @GetMapping("authRoleList.json")
    @ResponseBody
    TableResultData authRoleList(AuthRoleListPar authRoleListPar) {
        return sysUserService.authRoleList(authRoleListPar);
    }

    @GetMapping("menuManageList.json")
    @ResponseBody
    TableResultData menuManageList(MenuManageListPar menuManageListPar) {
        return sysUserService.menuManageList(menuManageListPar);
    }

    @GetMapping("authRoleTree.json")
    @ResponseBody
    AppResultData authRoleTree() {
        return sysUserService.authRoleTree();
    }

    @PostMapping("UpauthRole.json")
    @ResponseBody
    AppResultData UpauthRole(UPauthRolePar upauthRolePar) {
        return sysUserService.UpauthRole(upauthRolePar);
    }


    @PostMapping("OFFauthRole.json")
    @ResponseBody
    public AppResultData OFFauthRole(@Valid OFFauthRolePar offauthRolePar) {
        return sysUserService.OFFauthRole(offauthRolePar);
    }

    @PostMapping("DELauthRole.json")
    @ResponseBody
    public AppResultData DELauthRole(@Valid DELauthRolePar delauthRolePar) {
        return sysUserService.DELauthRole(delauthRolePar);
    }

    @PostMapping("addRole.json")
    @ResponseBody
    AppResultData addRole(@Valid AddRolePar addRolePar) {
        return sysUserService.addRole(addRolePar);
    }

    @PostMapping("ADDauthRole.json")
    @ResponseBody
    AppResultData ADDauthRole(@Valid ADDauthRolePar addauthRolePar) {
        return sysUserService.ADDauthRole(addauthRolePar);
    }

    @GetMapping("treeResource.json")
    @ResponseBody
    List<TreeResource> treeResource(String id) {
        return sysUserService.getTreeResourceListByusername(id);
    }

    @PostMapping("delRole.json")
    @ResponseBody
    AppResultData delRole(DelRolePar delRolePar) {
        return sysUserService.delRole(delRolePar);
    }

    @PostMapping("setRole.json")
    @ResponseBody
    AppResultData setRole(SetRolePar setRolePar) {
        return sysUserService.setRole(setRolePar);
    }

    @PostMapping("upRole.json")
    @ResponseBody
    AppResultData upRole(UpRolePar upRolePar) {
        return sysUserService.upRole(upRolePar);
    }

    @PostMapping("UPMenu.json")
    @ResponseBody
    AppResultData UPMenu(UPMenuPar uPMenuPar) {
        return sysUserService.UPMenu(uPMenuPar);
    }

    @GetMapping("AddMenuTree.json")
    @ResponseBody
    AppResultData AddMenuTree() {
        return sysUserService.AddMenuTree();
    }

    @RequestMapping("controller.json")
    @ResponseBody
    Object controller(HttpServletRequest httpServletRequest, String action) {
        return sysUserService.controller(httpServletRequest, action);
    }

    @PostMapping("modifyHtPass.json")
    @ResponseBody
    public AppResultData modifyHtPass(String oldPass, String newPass) {
        return sysUserService.modifyHtPass(oldPass, newPass);
    }

    @PostMapping("upimgs.json")
    @ResponseBody
    PicUpresult upimgs(MultipartFile file, String foldName) {
        return sysUserService.upimgs(file, foldName);
    }

    @GetMapping("getTopMenuList.json")
    @ResponseBody
    AppResultData getTopMenuList() {
        return sysUserService.getTopMenuList();
    }
}
